package com.chunuo.userservice.controller;


import com.chunuo.commonutils.JwtUtils;
import com.chunuo.commonutils.R;
import com.chunuo.commonutils.ordervo.UcenterMemberOrder;
import com.chunuo.userservice.entity.UcenterMember;
import com.chunuo.userservice.entity.vo.RegisterVo;
import com.chunuo.userservice.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-21
 */
@RestController
@RequestMapping("/userservice/ucenter-member")
public class UcenterMemberController {


    @Autowired
    private UcenterMemberService memberService;

    /**
     * 登录
     * @param member
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody UcenterMember member){
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //通过请求 获取用户的Id
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        //根据id查询用户返回
        UcenterMember userInfo = memberService.getById(userId);
        return R.ok().data("userInfo",userInfo);
    }

    /**
     * 根据用户id获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("getInfo/{userId}")
    public UcenterMemberOrder getInfo(@PathVariable("userId")String userId){
        //根据id查询用户返回
        UcenterMember userInfo = memberService.getById(userId);
        UcenterMemberOrder userOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(userInfo,userOrder);
        return userOrder;
    }

    /**
     * 获取当天注册的人数
     * @param day
     * @return
     */
    @GetMapping("getRegisterCount/{day}")
    public R getRegisterCount(@PathVariable("day")String day){
        Integer registerCount = memberService.getRegisterCount(day);
        return R.ok().data("registerCount",registerCount);
    }

}

